import os
import shutil

# 源目录
source_dir = '.'  # 当前目录，你可以根据需要修改

# 定义文件类型和对应的目标文件夹
file_type_mapping = {
    '.py': 'python_files',
    '.txt': 'text_files',
    '.csv': 'csv_files'
}

# 创建目标文件夹
for folder in file_type_mapping.values():
    if not os.path.exists(folder):
        os.makedirs(folder)

# 遍历源目录中的所有文件
for filename in os.listdir(source_dir):
    file_extension = os.path.splitext(filename)[1]
    if file_extension in file_type_mapping:
        source_file = os.path.join(source_dir, filename)
        target_folder = file_type_mapping[file_extension]
        target_file = os.path.join(target_folder, filename)
        # 移动文件
        shutil.move(source_file, target_file)
        print(f"Moved {filename} to {target_folder}")